﻿using WMM.Tapioca;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Xml;
using System.Configuration;
using System.Net;
using WMM.Tapioca.Instance;
using System;
using WMM.Tapioca.Utilities;

namespace WMM.Test.Tapioca
{
    /// <summary>
    /// Tests written against bugs filed in work items.
    /// </summary>
    [TestClass]
    public class WorkItemsTests
    {
        static XmlCachingResolver resolver;

        [ClassInitialize()]
        public static void MyClassInitialize(TestContext testContext)
        {
            bool useProxy = Boolean.Parse(ConfigurationManager.AppSettings["UseProxy"]);
            resolver = new XmlCachingResolver(true, useProxy);    
        }

        /// <summary>
        /// Work item 3828 contains several calculation arcs. Each of the calculation arcs uses a decimal number as the value of the "order"
        /// attribute. Section 3.5.3.9 of the XBRL 2.1 spec does indeed specify that the "order" attribute value is of type "decimal". Early versions
        /// of Tapioca assumed that this value was of an integer type; those early versions of Tapioca would throw an exception when trying
        /// to parse the decimal-based "order" attributes as an integer.
        /// </summary>
        [TestMethod]
        public void WorkItem3828Test()
        {
            var NewInstanceDocument = new InstanceDocument(@"..\..\..\WMM.Test.Tapioca\WorkItemsInput\WorkItem3828\cmi-20081231.xml", resolver);

            XbrlManager xbrlManager = new XbrlManager(NewInstanceDocument);            
            xbrlManager.Validate();

            Assert.IsTrue(NewInstanceDocument.Dts.ValidatedSchema, "The schema document was not validated.");
            Assert.IsTrue(NewInstanceDocument.Dts.ValidatedInstance, "The instance document was not validated.");
            Assert.IsTrue(NewInstanceDocument.Dts.ValidatedLinkbases, "The linkbase document was not validated.");
        }

        /// <summary>
        /// Work item 3903 notes that multiple namespace URIs can exist in an XML schema, and early versions of Tapioca didn't support that.
        /// </summary>
        [TestMethod]
        public void WorkItem3903Test()
        {
            var NewInstanceDocument = new InstanceDocument(@"..\..\..\WMM.Test.Tapioca\WorkItemsInput\WorkItem3903\WorkItem3903.xml", resolver);
            XbrlManager xbrlManager = new XbrlManager(NewInstanceDocument);            
            xbrlManager.Validate();
            Assert.IsTrue(NewInstanceDocument.Dts.ValidatedSchema, "The schema document was not validated.");
            Assert.IsTrue(NewInstanceDocument.Dts.ValidatedInstance, "The instance document was not validated.");
            Assert.IsTrue(NewInstanceDocument.Dts.ValidatedLinkbases, "The linkbase document was not validated.");
        }

		/// <summary>
		/// Work item 9401 notes that Tapioca throws a Null Reference Exception in the Qualified Name code when the associated
		/// document is loaded.
		/// </summary>
		[TestMethod]
		public void WorkItem9401Test()
		{
            var NewInstanceDocument = new InstanceDocument(@"..\..\..\WMM.Test.Tapioca\WorkItemsInput\WorkItem9401\amzn-20120331.xml", resolver);
            XbrlManager xbrlManager = new XbrlManager(NewInstanceDocument);            
            xbrlManager.Validate();
            Assert.IsTrue(NewInstanceDocument.Dts.ValidatedSchema, "The schema document was not validated.");
            Assert.IsTrue(NewInstanceDocument.Dts.ValidatedInstance, "The instance document was not validated.");
            Assert.IsTrue(NewInstanceDocument.Dts.ValidatedLinkbases, "The linkbase document was not validated.");
		}
	}
}
